package com.tech.view.component.input;

import com.tech.view.common.ColorUtil;
import com.tech.view.common.ResourcesUtil;
import com.tech.view.common.SwingTestUtils;
import com.tech.view.component.button.SimpleIconButton;
import org.jdesktop.swingx.util.GraphicsUtilities;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

/**
 * 输入框，支持左侧图标，提示，右侧图标按钮
 */
public class IconTextField extends JComponent {
	public static int DEFAULT_WIDTH = 250;
	public static int DEFAULT_HEIGHT = 36;
	public static int ICON_SIZE = 20;
	private JTextField textField;
	private JButton button;
	private int width, height;
	private Insets insets;
	private boolean isPassword;
	private BufferedImage leftIcon, rightIcon;
	private String placeholder;
	private Color placeholderColor = ColorUtil.SHALLOW_FONT_COLOR;
	private Font font;

	/**
	 * 创建默认大小的输入框实例
	 *
	 * @param isPassword 是否为密码类型
	 */
	public IconTextField(boolean isPassword) {
		this(DEFAULT_WIDTH, DEFAULT_HEIGHT, null, null, null, isPassword);
	}

	public IconTextField(int width, int height, boolean isPassword) {
		this(width, height, null, null, null, isPassword);
	}

	public IconTextField(BufferedImage leftIcon, String placeholder, boolean isPassword) {
		this(DEFAULT_WIDTH, DEFAULT_HEIGHT, leftIcon, null, placeholder, isPassword);
	}

	public IconTextField(BufferedImage leftIcon, BufferedImage rightIcon, String placeholder, boolean isPassword) {
		this(DEFAULT_WIDTH, DEFAULT_HEIGHT, leftIcon, rightIcon, placeholder, isPassword);
	}

	/**
	 * 创建输入框实例
	 *
	 * @param width       输入框宽度
	 * @param height      输入框高度
	 * @param leftIcon    左侧图标
	 * @param rightIcon   右侧图标，设置右侧图标，就应该稍后添加按钮的点击事件
	 * @param placeholder 提示文字
	 * @param isPassword  是否为密码类型
	 */
	public IconTextField(int width, int height, BufferedImage leftIcon, BufferedImage rightIcon, String placeholder, boolean isPassword) {
		this.width = width;
		this.height = height;
		this.leftIcon = leftIcon;
		this.rightIcon = rightIcon;
		this.placeholder = placeholder;
		this.isPassword = isPassword;

		init();
	}

	private void init(){
		// 图像
		if (leftIcon != null && Math.max(leftIcon.getWidth(), leftIcon.getHeight()) > ICON_SIZE)
			this.leftIcon = GraphicsUtilities.createThumbnailFast(leftIcon, ICON_SIZE);
		if (rightIcon != null && Math.max(rightIcon.getWidth(), rightIcon.getHeight()) > ICON_SIZE)
			this.rightIcon = GraphicsUtilities.createThumbnailFast(rightIcon, ICON_SIZE);
		// 创建对象
		if (isPassword) textField = new JPasswordField();
		else textField = new JTextField();

		// 设置位置和大小
		setPreferredSize(new Dimension(width, height));
		if (rightIcon != null)
			textField.setBounds(0, 0, width - 23, height);
		else
			textField.setBounds(0, 0, width, height);
		// 获取输入框的外边距，这里不知道为什么对象的insets更改后，再创建该对象时insets就是更改后的值，也许getMargin()返回的是一个静态对象
		// insets = textField.getMargin();
		insets = new Insets(2, 6, 2, 6);
		if (leftIcon != null) {
			insets.left = 28;
		}
		textField.setMargin(insets);
		// 字体
		font = textField.getFont().deriveFont((height - 2 * insets.top) / 2f);
		textField.setFont(font);
		add(textField);

		// 右侧按钮
		if (rightIcon != null) {
			button = new SimpleIconButton(new ImageIcon(this.rightIcon));
			button.setBounds(width - 23, 0, 23, height);
			add(button);
		}
	}

	public void hideRightButton() {
		if (rightIcon == null)
			return;
		textField.setBounds(0, 0, width, height);
		remove(button);
	}

	public void showRightButton() {
		if (rightIcon == null)
			return;
		textField.setBounds(0, 0, width - 23, height);
		add(button);
	}

	/**
	 * 添加右侧按钮的监听器
	 *
	 * @param l 监听器
	 */
	public void addButtonActionListener(ActionListener l) {
		button.addActionListener(l);
	}

	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		super.paint(g2);

		// 绘制左侧图标
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		if (leftIcon != null)
			g2.drawImage(leftIcon, insets.left - ICON_SIZE - 3, (height - ICON_SIZE) / 2, null);

		// 绘制文字
		if (placeholder != null && "".equals(textField.getText())) {
			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
			g2.setFont(font);
			g2.setColor(placeholderColor);
			FontRenderContext context = g2.getFontRenderContext();
			Rectangle2D bounds = font.getStringBounds(placeholder, context);
			g2.drawString(placeholder, this.insets.left, (int) (height + bounds.getHeight() / 2) / 2);
		}
	}

	public String getText() {
		if (isPassword)
			throw new RuntimeException("IconTextField 对象为密码类型，请调用getPassword()");
		else
			return textField.getText();
	}

	public char[] getPassword() {
		if (isPassword)
			return ((JPasswordField) textField).getPassword();
		else
			throw new RuntimeException("IconTextField 对象不是密码类型，无法调用getPassword()");
	}

	public void clear() {
		textField.setText("");
	}

	public static void main(String[] args) {
		SwingTestUtils.loadSkin();
		SwingTestUtils.test(new IconTextField(ResourcesUtil.getImage(ResourcesUtil.Image.LOGO),
				ResourcesUtil.getImage(ResourcesUtil.Image.LOGO),"登录",false));
	}
}
